package service

import (
	"singo/model"
	"singo/serializer"
)

// ManageTaskCheckService 待审核已审核管理列表服务
type ManageTaskCheckService struct {
	Limit        int  `form:"pageSize" json:"pageSize"`
	Start        int  `form:"pageNo" json:"pageNo"`
	Status       uint `form:"status" json:"status"`
	ReviewStatus uint `form:"review_status" json:"review_status"`
}

// ManageTaskCheckList 待审核已审核管理列表
func (service *ManageTaskCheckService) ManageTaskCheckList(userUID string) serializer.Response {
	tasks := []model.Task{}
	total := 0

	if service.Limit == 0 {
		service.Limit = 10
	}

	if err := model.DB.Model(&tasks).Where("review_status = ? AND review_by = ?", service.ReviewStatus, userUID).Where(&model.Task{Status: service.Status}).Count(&total).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("review_status = ? AND review_by = ?", service.ReviewStatus, userUID).Where(&model.Task{Status: service.Status}).Order("updated_at desc, review_status").Find(&tasks).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	return serializer.BuildListResponse(serializer.BuildTasksSimple(tasks), uint(total))
}
